#!/bin/bash
# Analyze Control Plane PCAP Files
# Converts and analyzes N2, N4, and SBI interface captures

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CAPTURES_DIR="../docs/5gc-interfaces/captures/control-plane"
OUTPUT_DIR="./analysis/control-plane"

echo "=== 5GC Control Plane Analysis ==="
echo "Script directory: $SCRIPT_DIR"
echo "Captures directory: $CAPTURES_DIR"
echo "Output directory: $OUTPUT_DIR"
echo

# Create output directory
mkdir -p "$OUTPUT_DIR"

# Function to analyze a PCAP file
analyze_pcap() {
    local input_file="$1"
    local output_file="$2"
    local description="$3"
    
    if [[ -f "$input_file" ]]; then
        echo "Analyzing $description..."
        echo "  Input: $input_file"
        echo "  Output: $output_file"
        
        # Convert to JSON with summary
        python3 "$SCRIPT_DIR/pcap-to-json.py" "$input_file" "$output_file" --summary
        
        echo "  ✓ Completed"
        echo
    else
        echo "⚠ Skipping $description - file not found: $input_file"
        echo
    fi
}

# Analyze N2 interface (gNodeB ↔ AMF)
analyze_pcap \
    "$CAPTURES_DIR/n2_amf_control.pcap" \
    "$OUTPUT_DIR/n2_amf_analysis.json" \
    "N2 Interface (gNodeB ↔ AMF)"

# Analyze N4 interface (SMF ↔ UPF)
analyze_pcap \
    "$CAPTURES_DIR/n4_smf_control.pcap" \
    "$OUTPUT_DIR/n4_smf_analysis.json" \
    "N4 Interface (SMF ↔ UPF)"

# Analyze SBI interface (5GC Functions)
analyze_pcap \
    "$CAPTURES_DIR/sbi_control.pcap" \
    "$OUTPUT_DIR/sbi_analysis.json" \
    "SBI Interface (5GC Functions)"

# Generate summary report
echo "=== Generating Summary Report ==="
cat > "$OUTPUT_DIR/summary_report.txt" << EOF
5GC Control Plane Analysis Summary
==================================
Generated: $(date)
Script: $0

Files Analyzed:
EOF

# Check which files were processed
for file in n2_amf_control.pcap n4_smf_control.pcap sbi_control.pcap; do
    if [[ -f "$CAPTURES_DIR/$file" ]]; then
        echo "✓ $file" >> "$OUTPUT_DIR/summary_report.txt"
    else
        echo "✗ $file (not found)" >> "$OUTPUT_DIR/summary_report.txt"
    fi
done

cat >> "$OUTPUT_DIR/summary_report.txt" << EOF

Output Files:
- n2_amf_analysis.json: N2 interface analysis (gNodeB ↔ AMF)
- n4_smf_analysis.json: N4 interface analysis (SMF ↔ UPF)  
- sbi_analysis.json: SBI interface analysis (5GC Functions)

Analysis Results:
EOF

# Add analysis results if files exist
for analysis_file in n2_amf_analysis.json n4_smf_analysis.json sbi_analysis.json; do
    if [[ -f "$OUTPUT_DIR/$analysis_file" ]]; then
        echo "" >> "$OUTPUT_DIR/summary_report.txt"
        echo "=== $analysis_file ===" >> "$OUTPUT_DIR/summary_report.txt"
        python3 -c "
import json
try:
    with open('$OUTPUT_DIR/$analysis_file', 'r') as f:
        data = json.load(f)
    print(f'Total packets: {data[\"metadata\"][\"total_packets\"]}')
    print(f'Protocols: {list(data[\"statistics\"][\"protocols\"].keys())}')
    print(f'Top source IPs: {list(data[\"statistics\"][\"source_ips\"].keys())[:3]}')
    print(f'Top dest IPs: {list(data[\"statistics\"][\"dest_ips\"].keys())[:3]}')
except Exception as e:
    print(f'Error reading file: {e}')
" >> "$OUTPUT_DIR/summary_report.txt"
    fi
done

echo "Summary report generated: $OUTPUT_DIR/summary_report.txt"
echo
echo "=== Analysis Complete ==="
echo "Results saved in: $OUTPUT_DIR"
echo
echo "To view detailed analysis:"
echo "  cat $OUTPUT_DIR/summary_report.txt"
echo "  jq '.' $OUTPUT_DIR/*.json" 